AOMedia AV1 Codec
av1_common_int.h
1/*
2 * Copyright (c) 2016, Alliance for Open Media. All rights reserved.
3 *
4 * This source code is subject to the terms of the BSD 2 Clause License and
5 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6 * was not distributed with this source code in the LICENSE file, you can
7 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8 * Media Patent License 1.0 was not distributed with this source code in the
9 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10 */
11
12#ifndef AOM_AV1_COMMON_AV1_COMMON_INT_H_
13#define AOM_AV1_COMMON_AV1_COMMON_INT_H_
14
15#include <stdbool.h>
16
17#include "config/aom_config.h"
18#include "config/av1_rtcd.h"
19
20#include "aom/internal/aom_codec_internal.h"
21#include "aom_dsp/flow_estimation/corner_detect.h"
22#include "aom_util/aom_pthread.h"
23#include "av1/common/alloccommon.h"
24#include "av1/common/av1_loopfilter.h"
25#include "av1/common/entropy.h"
26#include "av1/common/entropymode.h"
27#include "av1/common/entropymv.h"
28#include "av1/common/enums.h"
29#include "av1/common/frame_buffers.h"
30#include "av1/common/mv.h"
31#include "av1/common/quant_common.h"
33#include "av1/common/tile_common.h"
34#include "av1/common/timing.h"
35#include "aom_dsp/grain_params.h"
36#include "aom_dsp/grain_table.h"
37#include "aom_dsp/odintrin.h"
38#ifdef __cplusplus
39extern "C" {
40#endif
41
42#if defined(__clang__) && defined(__has_warning)
43#if __has_feature(cxx_attributes) && __has_warning("-Wimplicit-fallthrough")
44#define AOM_FALLTHROUGH_INTENDED [[clang::fallthrough]] // NOLINT
45#endif
46#elif defined(__GNUC__) && __GNUC__ >= 7
47#define AOM_FALLTHROUGH_INTENDED __attribute__((fallthrough)) // NOLINT
48#endif
49
50#ifndef AOM_FALLTHROUGH_INTENDED
51#define AOM_FALLTHROUGH_INTENDED \
52 do { \
53 } while (0)
54#endif
55
56#define CDEF_MAX_STRENGTHS 16
57
58/* Constant values while waiting for the sequence header */
59#define FRAME_ID_LENGTH 15
60#define DELTA_FRAME_ID_LENGTH 14
61
62#define FRAME_CONTEXTS (FRAME_BUFFERS + 1)
63// Extra frame context which is always kept at default values
64#define FRAME_CONTEXT_DEFAULTS (FRAME_CONTEXTS - 1)
65#define PRIMARY_REF_BITS 3
66#define PRIMARY_REF_NONE 7
67
68#define NUM_PING_PONG_BUFFERS 2
69
70#define MAX_NUM_TEMPORAL_LAYERS 8
71#define MAX_NUM_SPATIAL_LAYERS 4
72/* clang-format off */
73// clang-format seems to think this is a pointer dereference and not a
74// multiplication.
75#define MAX_NUM_OPERATING_POINTS \
76 (MAX_NUM_TEMPORAL_LAYERS * MAX_NUM_SPATIAL_LAYERS)
77/* clang-format on */
78
79// TODO(jingning): Turning this on to set up transform coefficient
80// processing timer.
81#define TXCOEFF_TIMER 0
82#define TXCOEFF_COST_TIMER 0
83
86enum {
87 SINGLE_REFERENCE = 0,
88 COMPOUND_REFERENCE = 1,
89 REFERENCE_MODE_SELECT = 2,
90 REFERENCE_MODES = 3,
91} UENUM1BYTE(REFERENCE_MODE);
92
93enum {
97 REFRESH_FRAME_CONTEXT_DISABLED,
102 REFRESH_FRAME_CONTEXT_BACKWARD,
103} UENUM1BYTE(REFRESH_FRAME_CONTEXT_MODE);
104
105#define MFMV_STACK_SIZE 3
106typedef struct {
107 int_mv mfmv0;
108 uint8_t ref_frame_offset;
109} TPL_MV_REF;
110
111typedef struct {
112 int_mv mv;
113 MV_REFERENCE_FRAME ref_frame;
114} MV_REF;
115
116typedef struct RefCntBuffer {
117 // For a RefCntBuffer, the following are reference-holding variables:
118 // - cm->ref_frame_map[]
119 // - cm->cur_frame
120 // - cm->scaled_ref_buf[] (encoder only)
121 // - pbi->output_frame_index[] (decoder only)
122 // With that definition, 'ref_count' is the number of reference-holding
123 // variables that are currently referencing this buffer.
124 // For example:
125 // - suppose this buffer is at index 'k' in the buffer pool, and
126 // - Total 'n' of the variables / array elements above have value 'k' (that
127 // is, they are pointing to buffer at index 'k').
128 // Then, pool->frame_bufs[k].ref_count = n.
129 int ref_count;
130
131 unsigned int order_hint;
132 unsigned int ref_order_hints[INTER_REFS_PER_FRAME];
133
134 // These variables are used only in encoder and compare the absolute
135 // display order hint to compute the relative distance and overcome
136 // the limitation of get_relative_dist() which returns incorrect
137 // distance when a very old frame is used as a reference.
138 unsigned int display_order_hint;
139 unsigned int ref_display_order_hint[INTER_REFS_PER_FRAME];
140 // Frame's level within the hierarchical structure.
141 unsigned int pyramid_level;
142 MV_REF *mvs;
143 uint8_t *seg_map;
144 struct segmentation seg;
145 int mi_rows;
146 int mi_cols;
147 // Width and height give the size of the buffer (before any upscaling, unlike
148 // the sizes that can be derived from the buf structure)
149 int width;
150 int height;
151 WarpedMotionParams global_motion[REF_FRAMES];
152 int showable_frame; // frame can be used as show existing frame in future
153 uint8_t film_grain_params_present;
154 aom_film_grain_t film_grain_params;
155 aom_codec_frame_buffer_t raw_frame_buffer;
157 int temporal_id; // Temporal layer ID of the frame
158 int spatial_id; // Spatial layer ID of the frame
159 FRAME_TYPE frame_type;
160
161 // This is only used in the encoder but needs to be indexed per ref frame
162 // so it's extremely convenient to keep it here.
163 int interp_filter_selected[SWITCHABLE];
164
165 // Inter frame reference frame delta for loop filter
166 int8_t ref_deltas[REF_FRAMES];
167
168 // 0 = ZERO_MV, MV
169 int8_t mode_deltas[MAX_MODE_LF_DELTAS];
170
171 FRAME_CONTEXT frame_context;
172
173 int filter_level[2];
174} RefCntBuffer;
175
176typedef struct BufferPool {
177// Protect BufferPool from being accessed by several FrameWorkers at
178// the same time during frame parallel decode.
179// TODO(hkuang): Try to use atomic variable instead of locking the whole pool.
180// TODO(wtc): Remove this. See
181// https://chromium-review.googlesource.com/c/webm/libvpx/+/560630.
182#if CONFIG_MULTITHREAD
183 pthread_mutex_t pool_mutex;
184#endif
185
186 // Private data associated with the frame buffer callbacks.
187 void *cb_priv;
188
191
192 RefCntBuffer *frame_bufs;
193 uint8_t num_frame_bufs;
194
195 // Frame buffers allocated internally by the codec.
196 InternalFrameBufferList int_frame_buffers;
197} BufferPool;
198
202typedef struct {
204 uint16_t *colbuf[MAX_MB_PLANE];
206 uint16_t *linebuf[MAX_MB_PLANE];
208 uint16_t *srcbuf;
210 size_t allocated_colbuf_size[MAX_MB_PLANE];
212 size_t allocated_linebuf_size[MAX_MB_PLANE];
220 int cdef_strengths[CDEF_MAX_STRENGTHS];
222 int cdef_uv_strengths[CDEF_MAX_STRENGTHS];
229} CdefInfo;
230
233typedef struct {
234 int delta_q_present_flag;
235 // Resolution of delta quant
236 int delta_q_res;
237 int delta_lf_present_flag;
238 // Resolution of delta lf level
239 int delta_lf_res;
240 // This is a flag for number of deltas of loop filter level
241 // 0: use 1 delta, for y_vertical, y_horizontal, u, and v
242 // 1: use separate deltas for each filter level
243 int delta_lf_multi;
244} DeltaQInfo;
245
246typedef struct {
247 int enable_order_hint; // 0 - disable order hint, and related tools
248 int order_hint_bits_minus_1; // dist_wtd_comp, ref_frame_mvs,
249 // frame_sign_bias
250 // if 0, enable_dist_wtd_comp and
251 // enable_ref_frame_mvs must be set as 0.
252 int enable_dist_wtd_comp; // 0 - disable dist-wtd compound modes
253 // 1 - enable it
254 int enable_ref_frame_mvs; // 0 - disable ref frame mvs
255 // 1 - enable it
256} OrderHintInfo;
257
258// Sequence header structure.
259// Note: All syntax elements of sequence_header_obu that need to be
260// bit-identical across multiple sequence headers must be part of this struct,
261// so that consistency is checked by are_seq_headers_consistent() function.
262// One exception is the last member 'op_params' that is ignored by
263// are_seq_headers_consistent() function.
264typedef struct SequenceHeader {
265 int num_bits_width;
266 int num_bits_height;
267 int max_frame_width;
268 int max_frame_height;
269 // Whether current and reference frame IDs are signaled in the bitstream.
270 // Frame id numbers are additional information that do not affect the
271 // decoding process, but provide decoders with a way of detecting missing
272 // reference frames so that appropriate action can be taken.
273 uint8_t frame_id_numbers_present_flag;
274 int frame_id_length;
275 int delta_frame_id_length;
276 BLOCK_SIZE sb_size; // Size of the superblock used for this frame
277 int mib_size; // Size of the superblock in units of MI blocks
278 int mib_size_log2; // Log 2 of above.
279
280 OrderHintInfo order_hint_info;
281
282 uint8_t force_screen_content_tools; // 0 - force off
283 // 1 - force on
284 // 2 - adaptive
285 uint8_t still_picture; // Video is a single frame still picture
286 uint8_t reduced_still_picture_hdr; // Use reduced header for still picture
287 uint8_t force_integer_mv; // 0 - Don't force. MV can use subpel
288 // 1 - force to integer
289 // 2 - adaptive
290 uint8_t enable_filter_intra; // enables/disables filterintra
291 uint8_t enable_intra_edge_filter; // enables/disables edge upsampling
292 uint8_t enable_interintra_compound; // enables/disables interintra_compound
293 uint8_t enable_masked_compound; // enables/disables masked compound
294 uint8_t enable_dual_filter; // 0 - disable dual interpolation filter
295 // 1 - enable vert/horz filter selection
296 uint8_t enable_warped_motion; // 0 - disable warp for the sequence
297 // 1 - enable warp for the sequence
298 uint8_t enable_superres; // 0 - Disable superres for the sequence
299 // and no frame level superres flag
300 // 1 - Enable superres for the sequence
301 // enable per-frame superres flag
302 uint8_t enable_cdef; // To turn on/off CDEF
303 uint8_t enable_restoration; // To turn on/off loop restoration
304 BITSTREAM_PROFILE profile;
305
306 // Color config.
307 aom_bit_depth_t bit_depth; // AOM_BITS_8 in profile 0 or 1,
308 // AOM_BITS_10 or AOM_BITS_12 in profile 2 or 3.
309 uint8_t use_highbitdepth; // If true, we need to use 16bit frame buffers.
310 uint8_t monochrome; // Monochrome video
311 aom_color_primaries_t color_primaries;
312 aom_transfer_characteristics_t transfer_characteristics;
313 aom_matrix_coefficients_t matrix_coefficients;
314 int color_range;
315 int subsampling_x; // Chroma subsampling for x
316 int subsampling_y; // Chroma subsampling for y
317 aom_chroma_sample_position_t chroma_sample_position;
318 uint8_t separate_uv_delta_q;
319 uint8_t film_grain_params_present;
320
321 // Operating point info.
322 int operating_points_cnt_minus_1;
323 int operating_point_idc[MAX_NUM_OPERATING_POINTS];
324 // True if operating_point_idc[op] is not equal to 0 for any value of op from
325 // 0 to operating_points_cnt_minus_1.
326 bool has_nonzero_operating_point_idc;
327 int timing_info_present;
328 aom_timing_info_t timing_info;
329 uint8_t decoder_model_info_present_flag;
330 aom_dec_model_info_t decoder_model_info;
331 uint8_t display_model_info_present_flag;
332 AV1_LEVEL seq_level_idx[MAX_NUM_OPERATING_POINTS];
333 uint8_t tier[MAX_NUM_OPERATING_POINTS]; // seq_tier in spec. One bit: 0 or 1.
334
335 // IMPORTANT: the op_params member must be at the end of the struct so that
336 // are_seq_headers_consistent() can be implemented with a memcmp() call.
337 // TODO(urvang): We probably don't need the +1 here.
338 aom_dec_model_op_parameters_t op_params[MAX_NUM_OPERATING_POINTS + 1];
339} SequenceHeader;
340
341typedef struct {
342 int skip_mode_allowed;
343 int skip_mode_flag;
344 int ref_frame_idx_0;
345 int ref_frame_idx_1;
346} SkipModeInfo;
347
348typedef struct {
349 FRAME_TYPE frame_type;
350 REFERENCE_MODE reference_mode;
351
352 unsigned int order_hint;
353 unsigned int display_order_hint;
354 // Frame's level within the hierarchical structure.
355 unsigned int pyramid_level;
356 unsigned int frame_number;
357 SkipModeInfo skip_mode_info;
358 int refresh_frame_flags; // Which ref frames are overwritten by this frame
359 int frame_refs_short_signaling;
360} CurrentFrame;
361
432
505
521
525 int MBs;
526
537
559 BLOCK_SIZE mi_alloc_bsize;
560
577
584 TX_TYPE *tx_type_map;
585
594 void (*free_mi)(struct CommonModeInfoParams *mi_params);
599 void (*setup_mi)(struct CommonModeInfoParams *mi_params);
609 void (*set_mb_mi)(struct CommonModeInfoParams *mi_params, int width,
610 int height, BLOCK_SIZE min_partition_size);
612};
613
623
629
638
649
650 /*
651 * Note: The qindex per superblock may have a delta from the qindex obtained
652 * at frame level from parameters above, based on 'cm->delta_q_info'.
653 */
654
662 int16_t y_dequant_QTX[MAX_SEGMENTS][2];
663 int16_t u_dequant_QTX[MAX_SEGMENTS][2];
664 int16_t v_dequant_QTX[MAX_SEGMENTS][2];
674 const qm_val_t *giqmatrix[NUM_QM_LEVELS][3][TX_SIZES_ALL];
678 const qm_val_t *gqmatrix[NUM_QM_LEVELS][3][TX_SIZES_ALL];
688 const qm_val_t *y_iqmatrix[MAX_SEGMENTS][TX_SIZES_ALL];
692 const qm_val_t *u_iqmatrix[MAX_SEGMENTS][TX_SIZES_ALL];
696 const qm_val_t *v_iqmatrix[MAX_SEGMENTS][TX_SIZES_ALL];
716};
717
718typedef struct CommonContexts CommonContexts;
727 PARTITION_CONTEXT **partition;
728
737 ENTROPY_CONTEXT **entropy[MAX_MB_PLANE];
738
745 TXFM_CONTEXT **txfm;
746
754};
755
759typedef struct AV1Common {
763 CurrentFrame current_frame;
767 struct aom_internal_error_info *error;
768
784 int width;
785 int height;
817
824 uint32_t buffer_removal_times[MAX_NUM_OPERATING_POINTS + 1];
831
835 RefCntBuffer *prev_frame;
836
841 RefCntBuffer *cur_frame;
842
863 int remapped_ref_idx[REF_FRAMES];
864
870 struct scale_factors sf_identity;
871
878 struct scale_factors ref_scale_factors[REF_FRAMES];
879
887 RefCntBuffer *ref_frame_map[REF_FRAMES];
888
895
903
910
915
920
921#if CONFIG_ENTROPY_STATS
925 int coef_cdf_category;
926#endif // CONFIG_ENTROPY_STATS
927
932
936 struct segmentation seg;
937
942
947 loop_filter_info_n lf_info;
948 struct loopfilter lf;
955 RestorationInfo rst_info[MAX_MB_PLANE];
956 int32_t *rst_tmpbuf;
957 RestorationLineBuffers *rlbs;
965
969 aom_film_grain_t film_grain_params;
970
974 DeltaQInfo delta_q_info;
975
979 WarpedMotionParams global_motion[REF_FRAMES];
980
985 SequenceHeader *seq_params;
986
990 FRAME_CONTEXT *fc;
996 FRAME_CONTEXT *default_frame_context;
997
1002
1006 BufferPool *buffer_pool;
1007
1015
1021 int ref_frame_id[REF_FRAMES];
1031 TPL_MV_REF *tpl_mvs;
1040 int ref_frame_sign_bias[REF_FRAMES];
1046 int8_t ref_frame_side[REF_FRAMES];
1047
1053
1059
1060#if TXCOEFF_TIMER
1061 int64_t cum_txcoeff_timer;
1062 int64_t txcoeff_timer;
1063 int txb_count;
1064#endif // TXCOEFF_TIMER
1065
1066#if TXCOEFF_COST_TIMER
1067 int64_t cum_txcoeff_cost_timer;
1068 int64_t txcoeff_cost_timer;
1069 int64_t txcoeff_cost_count;
1070#endif // TXCOEFF_COST_TIMER
1071} AV1_COMMON;
1072
1075// TODO(hkuang): Don't need to lock the whole pool after implementing atomic
1076// frame reference count.
1077static void lock_buffer_pool(BufferPool *const pool) {
1078#if CONFIG_MULTITHREAD
1079 pthread_mutex_lock(&pool->pool_mutex);
1080#else
1081 (void)pool;
1082#endif
1083}
1084
1085static void unlock_buffer_pool(BufferPool *const pool) {
1086#if CONFIG_MULTITHREAD
1087 pthread_mutex_unlock(&pool->pool_mutex);
1088#else
1089 (void)pool;
1090#endif
1091}
1092
1093static inline YV12_BUFFER_CONFIG *get_ref_frame(AV1_COMMON *cm, int index) {
1094 if (index < 0 || index >= REF_FRAMES) return NULL;
1095 if (cm->ref_frame_map[index] == NULL) return NULL;
1096 return &cm->ref_frame_map[index]->buf;
1097}
1098
1099static inline int get_free_fb(AV1_COMMON *cm) {
1100 RefCntBuffer *const frame_bufs = cm->buffer_pool->frame_bufs;
1101 int i;
1102
1103 lock_buffer_pool(cm->buffer_pool);
1104 const int num_frame_bufs = cm->buffer_pool->num_frame_bufs;
1105 for (i = 0; i < num_frame_bufs; ++i)
1106 if (frame_bufs[i].ref_count == 0) break;
1107
1108 if (i != num_frame_bufs) {
1109 if (frame_bufs[i].buf.use_external_reference_buffers) {
1110 // If this frame buffer's y_buffer, u_buffer, and v_buffer point to the
1111 // external reference buffers. Restore the buffer pointers to point to the
1112 // internally allocated memory.
1113 YV12_BUFFER_CONFIG *ybf = &frame_bufs[i].buf;
1114 ybf->y_buffer = ybf->store_buf_adr[0];
1115 ybf->u_buffer = ybf->store_buf_adr[1];
1116 ybf->v_buffer = ybf->store_buf_adr[2];
1117 ybf->use_external_reference_buffers = 0;
1118 }
1119
1120 frame_bufs[i].ref_count = 1;
1121 } else {
1122 // We should never run out of free buffers. If this assertion fails, there
1123 // is a reference leak.
1124 assert(0 && "Ran out of free frame buffers. Likely a reference leak.");
1125 // Reset i to be INVALID_IDX to indicate no free buffer found.
1126 i = INVALID_IDX;
1127 }
1128
1129 unlock_buffer_pool(cm->buffer_pool);
1130 return i;
1131}
1132
1133static inline RefCntBuffer *assign_cur_frame_new_fb(AV1_COMMON *const cm) {
1134 // Release the previously-used frame-buffer
1135 if (cm->cur_frame != NULL) {
1136 --cm->cur_frame->ref_count;
1137 cm->cur_frame = NULL;
1138 }
1139
1140 // Assign a new framebuffer
1141 const int new_fb_idx = get_free_fb(cm);
1142 if (new_fb_idx == INVALID_IDX) return NULL;
1143
1144 cm->cur_frame = &cm->buffer_pool->frame_bufs[new_fb_idx];
1145#if CONFIG_AV1_ENCODER && !CONFIG_REALTIME_ONLY
1146 aom_invalidate_pyramid(cm->cur_frame->buf.y_pyramid);
1147 av1_invalidate_corner_list(cm->cur_frame->buf.corners);
1148#endif // CONFIG_AV1_ENCODER && !CONFIG_REALTIME_ONLY
1149 av1_zero(cm->cur_frame->interp_filter_selected);
1150 return cm->cur_frame;
1151}
1152
1153// Modify 'lhs_ptr' to reference the buffer at 'rhs_ptr', and update the ref
1154// counts accordingly.
1155static inline void assign_frame_buffer_p(RefCntBuffer **lhs_ptr,
1156 RefCntBuffer *rhs_ptr) {
1157 RefCntBuffer *const old_ptr = *lhs_ptr;
1158 if (old_ptr != NULL) {
1159 assert(old_ptr->ref_count > 0);
1160 // One less reference to the buffer at 'old_ptr', so decrease ref count.
1161 --old_ptr->ref_count;
1162 }
1163
1164 *lhs_ptr = rhs_ptr;
1165 // One more reference to the buffer at 'rhs_ptr', so increase ref count.
1166 ++rhs_ptr->ref_count;
1167}
1168
1169static inline int frame_is_intra_only(const AV1_COMMON *const cm) {
1170 return cm->current_frame.frame_type == KEY_FRAME ||
1171 cm->current_frame.frame_type == INTRA_ONLY_FRAME;
1172}
1173
1174static inline int frame_is_sframe(const AV1_COMMON *cm) {
1175 return cm->current_frame.frame_type == S_FRAME;
1176}
1177
1178// These functions take a reference frame label between LAST_FRAME and
1179// EXTREF_FRAME inclusive. Note that this is different to the indexing
1180// previously used by the frame_refs[] array.
1181static inline int get_ref_frame_map_idx(const AV1_COMMON *const cm,
1182 const MV_REFERENCE_FRAME ref_frame) {
1183 return (ref_frame >= LAST_FRAME && ref_frame <= EXTREF_FRAME)
1184 ? cm->remapped_ref_idx[ref_frame - LAST_FRAME]
1185 : INVALID_IDX;
1186}
1187
1188static inline RefCntBuffer *get_ref_frame_buf(
1189 const AV1_COMMON *const cm, const MV_REFERENCE_FRAME ref_frame) {
1190 const int map_idx = get_ref_frame_map_idx(cm, ref_frame);
1191 return (map_idx != INVALID_IDX) ? cm->ref_frame_map[map_idx] : NULL;
1192}
1193
1194// Both const and non-const versions of this function are provided so that it
1195// can be used with a const AV1_COMMON if needed.
1196static inline const struct scale_factors *get_ref_scale_factors_const(
1197 const AV1_COMMON *const cm, const MV_REFERENCE_FRAME ref_frame) {
1198 const int map_idx = get_ref_frame_map_idx(cm, ref_frame);
1199 return (map_idx != INVALID_IDX) ? &cm->ref_scale_factors[map_idx] : NULL;
1200}
1201
1202static inline struct scale_factors *get_ref_scale_factors(
1203 AV1_COMMON *const cm, const MV_REFERENCE_FRAME ref_frame) {
1204 const int map_idx = get_ref_frame_map_idx(cm, ref_frame);
1205 return (map_idx != INVALID_IDX) ? &cm->ref_scale_factors[map_idx] : NULL;
1206}
1207
1208static inline RefCntBuffer *get_primary_ref_frame_buf(
1209 const AV1_COMMON *const cm) {
1210 const int primary_ref_frame = cm->features.primary_ref_frame;
1211 if (primary_ref_frame == PRIMARY_REF_NONE) return NULL;
1212 const int map_idx = get_ref_frame_map_idx(cm, primary_ref_frame + 1);
1213 return (map_idx != INVALID_IDX) ? cm->ref_frame_map[map_idx] : NULL;
1214}
1215
1216// Returns 1 if this frame might allow mvs from some reference frame.
1217static inline int frame_might_allow_ref_frame_mvs(const AV1_COMMON *cm) {
1218 return !cm->features.error_resilient_mode &&
1219 cm->seq_params->order_hint_info.enable_ref_frame_mvs &&
1220 cm->seq_params->order_hint_info.enable_order_hint &&
1221 !frame_is_intra_only(cm);
1222}
1223
1224// Returns 1 if this frame might use warped_motion
1225static inline int frame_might_allow_warped_motion(const AV1_COMMON *cm) {
1226 return !cm->features.error_resilient_mode && !frame_is_intra_only(cm) &&
1227 cm->seq_params->enable_warped_motion;
1228}
1229
1230static inline void ensure_mv_buffer(RefCntBuffer *buf, AV1_COMMON *cm) {
1231 const int buf_rows = buf->mi_rows;
1232 const int buf_cols = buf->mi_cols;
1233 const CommonModeInfoParams *const mi_params = &cm->mi_params;
1234
1235 if (buf->mvs == NULL || buf_rows != mi_params->mi_rows ||
1236 buf_cols != mi_params->mi_cols) {
1237 aom_free(buf->mvs);
1238 buf->mi_rows = mi_params->mi_rows;
1239 buf->mi_cols = mi_params->mi_cols;
1240 CHECK_MEM_ERROR(cm, buf->mvs,
1241 (MV_REF *)aom_calloc(((mi_params->mi_rows + 1) >> 1) *
1242 ((mi_params->mi_cols + 1) >> 1),
1243 sizeof(*buf->mvs)));
1244 aom_free(buf->seg_map);
1245 CHECK_MEM_ERROR(
1246 cm, buf->seg_map,
1247 (uint8_t *)aom_calloc(mi_params->mi_rows * mi_params->mi_cols,
1248 sizeof(*buf->seg_map)));
1249 }
1250
1251 const int mem_size =
1252 ((mi_params->mi_rows + MAX_MIB_SIZE) >> 1) * (mi_params->mi_stride >> 1);
1253
1254 if (cm->tpl_mvs == NULL || cm->tpl_mvs_mem_size < mem_size) {
1255 aom_free(cm->tpl_mvs);
1256 CHECK_MEM_ERROR(cm, cm->tpl_mvs,
1257 (TPL_MV_REF *)aom_calloc(mem_size, sizeof(*cm->tpl_mvs)));
1258 cm->tpl_mvs_mem_size = mem_size;
1259 }
1260}
1261
1262#if !CONFIG_REALTIME_ONLY || CONFIG_AV1_DECODER
1263void cfl_init(CFL_CTX *cfl, const SequenceHeader *seq_params);
1264#endif
1265
1266static inline int av1_num_planes(const AV1_COMMON *cm) {
1267 return cm->seq_params->monochrome ? 1 : MAX_MB_PLANE;
1268}
1269
1270static inline void av1_init_above_context(CommonContexts *above_contexts,
1271 int num_planes, int tile_row,
1272 MACROBLOCKD *xd) {
1273 for (int i = 0; i < num_planes; ++i) {
1274 xd->above_entropy_context[i] = above_contexts->entropy[i][tile_row];
1275 }
1276 xd->above_partition_context = above_contexts->partition[tile_row];
1277 xd->above_txfm_context = above_contexts->txfm[tile_row];
1278}
1279
1280static inline void av1_init_macroblockd(AV1_COMMON *cm, MACROBLOCKD *xd) {
1281 const int num_planes = av1_num_planes(cm);
1282 const CommonQuantParams *const quant_params = &cm->quant_params;
1283
1284 for (int i = 0; i < num_planes; ++i) {
1285 if (xd->plane[i].plane_type == PLANE_TYPE_Y) {
1286 memcpy(xd->plane[i].seg_dequant_QTX, quant_params->y_dequant_QTX,
1287 sizeof(quant_params->y_dequant_QTX));
1288 memcpy(xd->plane[i].seg_iqmatrix, quant_params->y_iqmatrix,
1289 sizeof(quant_params->y_iqmatrix));
1290
1291 } else {
1292 if (i == AOM_PLANE_U) {
1293 memcpy(xd->plane[i].seg_dequant_QTX, quant_params->u_dequant_QTX,
1294 sizeof(quant_params->u_dequant_QTX));
1295 memcpy(xd->plane[i].seg_iqmatrix, quant_params->u_iqmatrix,
1296 sizeof(quant_params->u_iqmatrix));
1297 } else {
1298 memcpy(xd->plane[i].seg_dequant_QTX, quant_params->v_dequant_QTX,
1299 sizeof(quant_params->v_dequant_QTX));
1300 memcpy(xd->plane[i].seg_iqmatrix, quant_params->v_iqmatrix,
1301 sizeof(quant_params->v_iqmatrix));
1302 }
1303 }
1304 }
1305 xd->mi_stride = cm->mi_params.mi_stride;
1306 xd->error_info = cm->error;
1307#if !CONFIG_REALTIME_ONLY || CONFIG_AV1_DECODER
1308 cfl_init(&xd->cfl, cm->seq_params);
1309#endif
1310}
1311
1312static inline void set_entropy_context(MACROBLOCKD *xd, int mi_row, int mi_col,
1313 const int num_planes) {
1314 int i;
1315 int row_offset = mi_row;
1316 int col_offset = mi_col;
1317 for (i = 0; i < num_planes; ++i) {
1318 struct macroblockd_plane *const pd = &xd->plane[i];
1319 // Offset the buffer pointer
1320 const BLOCK_SIZE bsize = xd->mi[0]->bsize;
1321 if (pd->subsampling_y && (mi_row & 0x01) && (mi_size_high[bsize] == 1))
1322 row_offset = mi_row - 1;
1323 if (pd->subsampling_x && (mi_col & 0x01) && (mi_size_wide[bsize] == 1))
1324 col_offset = mi_col - 1;
1325 int above_idx = col_offset;
1326 int left_idx = row_offset & MAX_MIB_MASK;
1327 pd->above_entropy_context =
1328 &xd->above_entropy_context[i][above_idx >> pd->subsampling_x];
1329 pd->left_entropy_context =
1330 &xd->left_entropy_context[i][left_idx >> pd->subsampling_y];
1331 }
1332}
1333
1334static inline int calc_mi_size(int len) {
1335 // len is in mi units. Align to a multiple of SBs.
1336 return ALIGN_POWER_OF_TWO(len, MAX_MIB_SIZE_LOG2);
1337}
1338
1339static inline void set_plane_n4(MACROBLOCKD *const xd, int bw, int bh,
1340 const int num_planes) {
1341 int i;
1342 for (i = 0; i < num_planes; i++) {
1343 xd->plane[i].width = (bw * MI_SIZE) >> xd->plane[i].subsampling_x;
1344 xd->plane[i].height = (bh * MI_SIZE) >> xd->plane[i].subsampling_y;
1345
1346 xd->plane[i].width = AOMMAX(xd->plane[i].width, 4);
1347 xd->plane[i].height = AOMMAX(xd->plane[i].height, 4);
1348 }
1349}
1350
1351static inline void set_mi_row_col(MACROBLOCKD *xd, const TileInfo *const tile,
1352 int mi_row, int bh, int mi_col, int bw,
1353 int mi_rows, int mi_cols) {
1354 xd->mb_to_top_edge = -GET_MV_SUBPEL(mi_row * MI_SIZE);
1355 xd->mb_to_bottom_edge = GET_MV_SUBPEL((mi_rows - bh - mi_row) * MI_SIZE);
1356 xd->mb_to_left_edge = -GET_MV_SUBPEL((mi_col * MI_SIZE));
1357 xd->mb_to_right_edge = GET_MV_SUBPEL((mi_cols - bw - mi_col) * MI_SIZE);
1358
1359 xd->mi_row = mi_row;
1360 xd->mi_col = mi_col;
1361
1362 // Are edges available for intra prediction?
1363 xd->up_available = (mi_row > tile->mi_row_start);
1364
1365 const int ss_x = xd->plane[1].subsampling_x;
1366 const int ss_y = xd->plane[1].subsampling_y;
1367
1368 xd->left_available = (mi_col > tile->mi_col_start);
1371 if (ss_x && bw < mi_size_wide[BLOCK_8X8])
1372 xd->chroma_left_available = (mi_col - 1) > tile->mi_col_start;
1373 if (ss_y && bh < mi_size_high[BLOCK_8X8])
1374 xd->chroma_up_available = (mi_row - 1) > tile->mi_row_start;
1375 if (xd->up_available) {
1376 xd->above_mbmi = xd->mi[-xd->mi_stride];
1377 } else {
1378 xd->above_mbmi = NULL;
1379 }
1380
1381 if (xd->left_available) {
1382 xd->left_mbmi = xd->mi[-1];
1383 } else {
1384 xd->left_mbmi = NULL;
1385 }
1386
1387 const int chroma_ref = ((mi_row & 0x01) || !(bh & 0x01) || !ss_y) &&
1388 ((mi_col & 0x01) || !(bw & 0x01) || !ss_x);
1389 xd->is_chroma_ref = chroma_ref;
1390 if (chroma_ref) {
1391 // To help calculate the "above" and "left" chroma blocks, note that the
1392 // current block may cover multiple luma blocks (e.g., if partitioned into
1393 // 4x4 luma blocks).
1394 // First, find the top-left-most luma block covered by this chroma block
1395 MB_MODE_INFO **base_mi =
1396 &xd->mi[-(mi_row & ss_y) * xd->mi_stride - (mi_col & ss_x)];
1397
1398 // Then, we consider the luma region covered by the left or above 4x4 chroma
1399 // prediction. We want to point to the chroma reference block in that
1400 // region, which is the bottom-right-most mi unit.
1401 // This leads to the following offsets:
1402 MB_MODE_INFO *chroma_above_mi =
1403 xd->chroma_up_available ? base_mi[-xd->mi_stride + ss_x] : NULL;
1404 xd->chroma_above_mbmi = chroma_above_mi;
1405
1406 MB_MODE_INFO *chroma_left_mi =
1407 xd->chroma_left_available ? base_mi[ss_y * xd->mi_stride - 1] : NULL;
1408 xd->chroma_left_mbmi = chroma_left_mi;
1409 }
1410
1411 xd->height = bh;
1412 xd->width = bw;
1413
1414 xd->is_last_vertical_rect = 0;
1415 if (xd->width < xd->height) {
1416 if (!((mi_col + xd->width) & (xd->height - 1))) {
1417 xd->is_last_vertical_rect = 1;
1418 }
1419 }
1420
1422 if (xd->width > xd->height)
1423 if (!(mi_row & (xd->width - 1))) xd->is_first_horizontal_rect = 1;
1424}
1425
1426static inline aom_cdf_prob *get_y_mode_cdf(FRAME_CONTEXT *tile_ctx,
1427 const MB_MODE_INFO *above_mi,
1428 const MB_MODE_INFO *left_mi) {
1429 const PREDICTION_MODE above = av1_above_block_mode(above_mi);
1430 const PREDICTION_MODE left = av1_left_block_mode(left_mi);
1431 const int above_ctx = intra_mode_context[above];
1432 const int left_ctx = intra_mode_context[left];
1433 return tile_ctx->kf_y_cdf[above_ctx][left_ctx];
1434}
1435
1436static inline void update_partition_context(MACROBLOCKD *xd, int mi_row,
1437 int mi_col, BLOCK_SIZE subsize,
1438 BLOCK_SIZE bsize) {
1439 PARTITION_CONTEXT *const above_ctx = xd->above_partition_context + mi_col;
1440 PARTITION_CONTEXT *const left_ctx =
1441 xd->left_partition_context + (mi_row & MAX_MIB_MASK);
1442
1443 const int bw = mi_size_wide[bsize];
1444 const int bh = mi_size_high[bsize];
1445 memset(above_ctx, partition_context_lookup[subsize].above, bw);
1446 memset(left_ctx, partition_context_lookup[subsize].left, bh);
1447}
1448
1449static inline int is_chroma_reference(int mi_row, int mi_col, BLOCK_SIZE bsize,
1450 int subsampling_x, int subsampling_y) {
1451 assert(bsize < BLOCK_SIZES_ALL);
1452 const int bw = mi_size_wide[bsize];
1453 const int bh = mi_size_high[bsize];
1454 int ref_pos = ((mi_row & 0x01) || !(bh & 0x01) || !subsampling_y) &&
1455 ((mi_col & 0x01) || !(bw & 0x01) || !subsampling_x);
1456 return ref_pos;
1457}
1458
1459static inline aom_cdf_prob cdf_element_prob(const aom_cdf_prob *cdf,
1460 size_t element) {
1461 assert(cdf != NULL);
1462 return (element > 0 ? cdf[element - 1] : CDF_PROB_TOP) - cdf[element];
1463}
1464
1465static inline void partition_gather_horz_alike(aom_cdf_prob *out,
1466 const aom_cdf_prob *const in,
1467 BLOCK_SIZE bsize) {
1468 (void)bsize;
1469 out[0] = CDF_PROB_TOP;
1470 out[0] -= cdf_element_prob(in, PARTITION_HORZ);
1471 out[0] -= cdf_element_prob(in, PARTITION_SPLIT);
1472 out[0] -= cdf_element_prob(in, PARTITION_HORZ_A);
1473 out[0] -= cdf_element_prob(in, PARTITION_HORZ_B);
1474 out[0] -= cdf_element_prob(in, PARTITION_VERT_A);
1475 if (bsize != BLOCK_128X128) out[0] -= cdf_element_prob(in, PARTITION_HORZ_4);
1476 out[0] = AOM_ICDF(out[0]);
1477 out[1] = AOM_ICDF(CDF_PROB_TOP);
1478}
1479
1480static inline void partition_gather_vert_alike(aom_cdf_prob *out,
1481 const aom_cdf_prob *const in,
1482 BLOCK_SIZE bsize) {
1483 (void)bsize;
1484 out[0] = CDF_PROB_TOP;
1485 out[0] -= cdf_element_prob(in, PARTITION_VERT);
1486 out[0] -= cdf_element_prob(in, PARTITION_SPLIT);
1487 out[0] -= cdf_element_prob(in, PARTITION_HORZ_A);
1488 out[0] -= cdf_element_prob(in, PARTITION_VERT_A);
1489 out[0] -= cdf_element_prob(in, PARTITION_VERT_B);
1490 if (bsize != BLOCK_128X128) out[0] -= cdf_element_prob(in, PARTITION_VERT_4);
1491 out[0] = AOM_ICDF(out[0]);
1492 out[1] = AOM_ICDF(CDF_PROB_TOP);
1493}
1494
1495static inline void update_ext_partition_context(MACROBLOCKD *xd, int mi_row,
1496 int mi_col, BLOCK_SIZE subsize,
1497 BLOCK_SIZE bsize,
1498 PARTITION_TYPE partition) {
1499 if (bsize >= BLOCK_8X8) {
1500 const int hbs = mi_size_wide[bsize] / 2;
1501 BLOCK_SIZE bsize2 = get_partition_subsize(bsize, PARTITION_SPLIT);
1502 switch (partition) {
1503 case PARTITION_SPLIT:
1504 if (bsize != BLOCK_8X8) break;
1505 AOM_FALLTHROUGH_INTENDED;
1506 case PARTITION_NONE:
1507 case PARTITION_HORZ:
1508 case PARTITION_VERT:
1509 case PARTITION_HORZ_4:
1510 case PARTITION_VERT_4:
1511 update_partition_context(xd, mi_row, mi_col, subsize, bsize);
1512 break;
1513 case PARTITION_HORZ_A:
1514 update_partition_context(xd, mi_row, mi_col, bsize2, subsize);
1515 update_partition_context(xd, mi_row + hbs, mi_col, subsize, subsize);
1516 break;
1517 case PARTITION_HORZ_B:
1518 update_partition_context(xd, mi_row, mi_col, subsize, subsize);
1519 update_partition_context(xd, mi_row + hbs, mi_col, bsize2, subsize);
1520 break;
1521 case PARTITION_VERT_A:
1522 update_partition_context(xd, mi_row, mi_col, bsize2, subsize);
1523 update_partition_context(xd, mi_row, mi_col + hbs, subsize, subsize);
1524 break;
1525 case PARTITION_VERT_B:
1526 update_partition_context(xd, mi_row, mi_col, subsize, subsize);
1527 update_partition_context(xd, mi_row, mi_col + hbs, bsize2, subsize);
1528 break;
1529 default: assert(0 && "Invalid partition type");
1530 }
1531 }
1532}
1533
1534static inline int partition_plane_context(const MACROBLOCKD *xd, int mi_row,
1535 int mi_col, BLOCK_SIZE bsize) {
1536 const PARTITION_CONTEXT *above_ctx = xd->above_partition_context + mi_col;
1537 const PARTITION_CONTEXT *left_ctx =
1538 xd->left_partition_context + (mi_row & MAX_MIB_MASK);
1539 // Minimum partition point is 8x8. Offset the bsl accordingly.
1540 const int bsl = mi_size_wide_log2[bsize] - mi_size_wide_log2[BLOCK_8X8];
1541 int above = (*above_ctx >> bsl) & 1, left = (*left_ctx >> bsl) & 1;
1542
1543 assert(mi_size_wide_log2[bsize] == mi_size_high_log2[bsize]);
1544 assert(bsl >= 0);
1545
1546 return (left * 2 + above) + bsl * PARTITION_PLOFFSET;
1547}
1548
1549// Return the number of elements in the partition CDF when
1550// partitioning the (square) block with luma block size of bsize.
1551static inline int partition_cdf_length(BLOCK_SIZE bsize) {
1552 if (bsize <= BLOCK_8X8)
1553 return PARTITION_TYPES;
1554 else if (bsize == BLOCK_128X128)
1555 return EXT_PARTITION_TYPES - 2;
1556 else
1557 return EXT_PARTITION_TYPES;
1558}
1559
1560static inline int max_block_wide(const MACROBLOCKD *xd, BLOCK_SIZE bsize,
1561 int plane) {
1562 assert(bsize < BLOCK_SIZES_ALL);
1563 int max_blocks_wide = block_size_wide[bsize];
1564
1565 if (xd->mb_to_right_edge < 0) {
1566 const struct macroblockd_plane *const pd = &xd->plane[plane];
1567 max_blocks_wide += xd->mb_to_right_edge >> (3 + pd->subsampling_x);
1568 }
1569
1570 // Scale the width in the transform block unit.
1571 return max_blocks_wide >> MI_SIZE_LOG2;
1572}
1573
1574static inline int max_block_high(const MACROBLOCKD *xd, BLOCK_SIZE bsize,
1575 int plane) {
1576 int max_blocks_high = block_size_high[bsize];
1577
1578 if (xd->mb_to_bottom_edge < 0) {
1579 const struct macroblockd_plane *const pd = &xd->plane[plane];
1580 max_blocks_high += xd->mb_to_bottom_edge >> (3 + pd->subsampling_y);
1581 }
1582
1583 // Scale the height in the transform block unit.
1584 return max_blocks_high >> MI_SIZE_LOG2;
1585}
1586
1587static inline void av1_zero_above_context(AV1_COMMON *const cm,
1588 const MACROBLOCKD *xd,
1589 int mi_col_start, int mi_col_end,
1590 const int tile_row) {
1591 const SequenceHeader *const seq_params = cm->seq_params;
1592 const int num_planes = av1_num_planes(cm);
1593 const int width = mi_col_end - mi_col_start;
1594 const int aligned_width =
1595 ALIGN_POWER_OF_TWO(width, seq_params->mib_size_log2);
1596 const int offset_y = mi_col_start;
1597 const int width_y = aligned_width;
1598 const int offset_uv = offset_y >> seq_params->subsampling_x;
1599 const int width_uv = width_y >> seq_params->subsampling_x;
1600 CommonContexts *const above_contexts = &cm->above_contexts;
1601
1602 av1_zero_array(above_contexts->entropy[0][tile_row] + offset_y, width_y);
1603 if (num_planes > 1) {
1604 if (above_contexts->entropy[1][tile_row] &&
1605 above_contexts->entropy[2][tile_row]) {
1606 av1_zero_array(above_contexts->entropy[1][tile_row] + offset_uv,
1607 width_uv);
1608 av1_zero_array(above_contexts->entropy[2][tile_row] + offset_uv,
1609 width_uv);
1610 } else {
1611 aom_internal_error(xd->error_info, AOM_CODEC_CORRUPT_FRAME,
1612 "Invalid value of planes");
1613 }
1614 }
1615
1616 av1_zero_array(above_contexts->partition[tile_row] + mi_col_start,
1617 aligned_width);
1618
1619 memset(above_contexts->txfm[tile_row] + mi_col_start,
1620 tx_size_wide[TX_SIZES_LARGEST], aligned_width * sizeof(TXFM_CONTEXT));
1621}
1622
1623static inline void av1_zero_left_context(MACROBLOCKD *const xd) {
1624 av1_zero(xd->left_entropy_context);
1625 av1_zero(xd->left_partition_context);
1626
1627 memset(xd->left_txfm_context_buffer, tx_size_high[TX_SIZES_LARGEST],
1628 sizeof(xd->left_txfm_context_buffer));
1629}
1630
1631static inline void set_txfm_ctx(TXFM_CONTEXT *txfm_ctx, uint8_t txs, int len) {
1632 int i;
1633 for (i = 0; i < len; ++i) txfm_ctx[i] = txs;
1634}
1635
1636static inline void set_txfm_ctxs(TX_SIZE tx_size, int n4_w, int n4_h, int skip,
1637 const MACROBLOCKD *xd) {
1638 uint8_t bw = tx_size_wide[tx_size];
1639 uint8_t bh = tx_size_high[tx_size];
1640
1641 if (skip) {
1642 bw = n4_w * MI_SIZE;
1643 bh = n4_h * MI_SIZE;
1644 }
1645
1646 set_txfm_ctx(xd->above_txfm_context, bw, n4_w);
1647 set_txfm_ctx(xd->left_txfm_context, bh, n4_h);
1648}
1649
1650static inline int get_mi_grid_idx(const CommonModeInfoParams *const mi_params,
1651 int mi_row, int mi_col) {
1652 return mi_row * mi_params->mi_stride + mi_col;
1653}
1654
1655static inline int get_alloc_mi_idx(const CommonModeInfoParams *const mi_params,
1656 int mi_row, int mi_col) {
1657 const int mi_alloc_size_1d = mi_size_wide[mi_params->mi_alloc_bsize];
1658 const int mi_alloc_row = mi_row / mi_alloc_size_1d;
1659 const int mi_alloc_col = mi_col / mi_alloc_size_1d;
1660
1661 return mi_alloc_row * mi_params->mi_alloc_stride + mi_alloc_col;
1662}
1663
1664// For this partition block, set pointers in mi_params->mi_grid_base and xd->mi.
1665static inline void set_mi_offsets(const CommonModeInfoParams *const mi_params,
1666 MACROBLOCKD *const xd, int mi_row,
1667 int mi_col) {
1668 // 'mi_grid_base' should point to appropriate memory in 'mi'.
1669 const int mi_grid_idx = get_mi_grid_idx(mi_params, mi_row, mi_col);
1670 const int mi_alloc_idx = get_alloc_mi_idx(mi_params, mi_row, mi_col);
1671 mi_params->mi_grid_base[mi_grid_idx] = &mi_params->mi_alloc[mi_alloc_idx];
1672 // 'xd->mi' should point to an offset in 'mi_grid_base';
1673 xd->mi = mi_params->mi_grid_base + mi_grid_idx;
1674 // 'xd->tx_type_map' should point to an offset in 'mi_params->tx_type_map'.
1675 xd->tx_type_map = mi_params->tx_type_map + mi_grid_idx;
1676 xd->tx_type_map_stride = mi_params->mi_stride;
1677}
1678
1679static inline void txfm_partition_update(TXFM_CONTEXT *above_ctx,
1680 TXFM_CONTEXT *left_ctx,
1681 TX_SIZE tx_size, TX_SIZE txb_size) {
1682 BLOCK_SIZE bsize = txsize_to_bsize[txb_size];
1683 int bh = mi_size_high[bsize];
1684 int bw = mi_size_wide[bsize];
1685 uint8_t txw = tx_size_wide[tx_size];
1686 uint8_t txh = tx_size_high[tx_size];
1687 int i;
1688 for (i = 0; i < bh; ++i) left_ctx[i] = txh;
1689 for (i = 0; i < bw; ++i) above_ctx[i] = txw;
1690}
1691
1692static inline TX_SIZE get_sqr_tx_size(int tx_dim) {
1693 switch (tx_dim) {
1694 case 128:
1695 case 64: return TX_64X64; break;
1696 case 32: return TX_32X32; break;
1697 case 16: return TX_16X16; break;
1698 case 8: return TX_8X8; break;
1699 default: return TX_4X4;
1700 }
1701}
1702
1703static inline TX_SIZE get_tx_size(int width, int height) {
1704 if (width == height) {
1705 return get_sqr_tx_size(width);
1706 }
1707 if (width < height) {
1708 if (width + width == height) {
1709 switch (width) {
1710 case 4: return TX_4X8; break;
1711 case 8: return TX_8X16; break;
1712 case 16: return TX_16X32; break;
1713 case 32: return TX_32X64; break;
1714 }
1715 } else {
1716 switch (width) {
1717 case 4: return TX_4X16; break;
1718 case 8: return TX_8X32; break;
1719 case 16: return TX_16X64; break;
1720 }
1721 }
1722 } else {
1723 if (height + height == width) {
1724 switch (height) {
1725 case 4: return TX_8X4; break;
1726 case 8: return TX_16X8; break;
1727 case 16: return TX_32X16; break;
1728 case 32: return TX_64X32; break;
1729 }
1730 } else {
1731 switch (height) {
1732 case 4: return TX_16X4; break;
1733 case 8: return TX_32X8; break;
1734 case 16: return TX_64X16; break;
1735 }
1736 }
1737 }
1738 assert(0);
1739 return TX_4X4;
1740}
1741
1742static inline int txfm_partition_context(const TXFM_CONTEXT *const above_ctx,
1743 const TXFM_CONTEXT *const left_ctx,
1744 BLOCK_SIZE bsize, TX_SIZE tx_size) {
1745 const uint8_t txw = tx_size_wide[tx_size];
1746 const uint8_t txh = tx_size_high[tx_size];
1747 const int above = *above_ctx < txw;
1748 const int left = *left_ctx < txh;
1749 int category = TXFM_PARTITION_CONTEXTS;
1750
1751 // dummy return, not used by others.
1752 if (tx_size <= TX_4X4) return 0;
1753
1754 TX_SIZE max_tx_size =
1755 get_sqr_tx_size(AOMMAX(block_size_wide[bsize], block_size_high[bsize]));
1756
1757 if (max_tx_size >= TX_8X8) {
1758 category =
1759 (txsize_sqr_up_map[tx_size] != max_tx_size && max_tx_size > TX_8X8) +
1760 (TX_SIZES - 1 - max_tx_size) * 2;
1761 }
1762 assert(category != TXFM_PARTITION_CONTEXTS);
1763 return category * 3 + above + left;
1764}
1765
1766// Compute the next partition in the direction of the sb_type stored in the mi
1767// array, starting with bsize.
1768static inline PARTITION_TYPE get_partition(const AV1_COMMON *const cm,
1769 int mi_row, int mi_col,
1770 BLOCK_SIZE bsize) {
1771 const CommonModeInfoParams *const mi_params = &cm->mi_params;
1772 if (mi_row >= mi_params->mi_rows || mi_col >= mi_params->mi_cols)
1773 return PARTITION_INVALID;
1774
1775 const int offset = mi_row * mi_params->mi_stride + mi_col;
1776 MB_MODE_INFO **mi = mi_params->mi_grid_base + offset;
1777 const BLOCK_SIZE subsize = mi[0]->bsize;
1778
1779 assert(bsize < BLOCK_SIZES_ALL);
1780
1781 if (subsize == bsize) return PARTITION_NONE;
1782
1783 const int bhigh = mi_size_high[bsize];
1784 const int bwide = mi_size_wide[bsize];
1785 const int sshigh = mi_size_high[subsize];
1786 const int sswide = mi_size_wide[subsize];
1787
1788 if (bsize > BLOCK_8X8 && mi_row + bwide / 2 < mi_params->mi_rows &&
1789 mi_col + bhigh / 2 < mi_params->mi_cols) {
1790 // In this case, the block might be using an extended partition
1791 // type.
1792 const MB_MODE_INFO *const mbmi_right = mi[bwide / 2];
1793 const MB_MODE_INFO *const mbmi_below = mi[bhigh / 2 * mi_params->mi_stride];
1794
1795 if (sswide == bwide) {
1796 // Smaller height but same width. Is PARTITION_HORZ_4, PARTITION_HORZ or
1797 // PARTITION_HORZ_B. To distinguish the latter two, check if the lower
1798 // half was split.
1799 if (sshigh * 4 == bhigh) return PARTITION_HORZ_4;
1800 assert(sshigh * 2 == bhigh);
1801
1802 if (mbmi_below->bsize == subsize)
1803 return PARTITION_HORZ;
1804 else
1805 return PARTITION_HORZ_B;
1806 } else if (sshigh == bhigh) {
1807 // Smaller width but same height. Is PARTITION_VERT_4, PARTITION_VERT or
1808 // PARTITION_VERT_B. To distinguish the latter two, check if the right
1809 // half was split.
1810 if (sswide * 4 == bwide) return PARTITION_VERT_4;
1811 assert(sswide * 2 == bwide);
1812
1813 if (mbmi_right->bsize == subsize)
1814 return PARTITION_VERT;
1815 else
1816 return PARTITION_VERT_B;
1817 } else {
1818 // Smaller width and smaller height. Might be PARTITION_SPLIT or could be
1819 // PARTITION_HORZ_A or PARTITION_VERT_A. If subsize isn't halved in both
1820 // dimensions, we immediately know this is a split (which will recurse to
1821 // get to subsize). Otherwise look down and to the right. With
1822 // PARTITION_VERT_A, the right block will have height bhigh; with
1823 // PARTITION_HORZ_A, the lower block with have width bwide. Otherwise
1824 // it's PARTITION_SPLIT.
1825 if (sswide * 2 != bwide || sshigh * 2 != bhigh) return PARTITION_SPLIT;
1826
1827 if (mi_size_wide[mbmi_below->bsize] == bwide) return PARTITION_HORZ_A;
1828 if (mi_size_high[mbmi_right->bsize] == bhigh) return PARTITION_VERT_A;
1829
1830 return PARTITION_SPLIT;
1831 }
1832 }
1833 const int vert_split = sswide < bwide;
1834 const int horz_split = sshigh < bhigh;
1835 const int split_idx = (vert_split << 1) | horz_split;
1836 assert(split_idx != 0);
1837
1838 static const PARTITION_TYPE base_partitions[4] = {
1839 PARTITION_INVALID, PARTITION_HORZ, PARTITION_VERT, PARTITION_SPLIT
1840 };
1841
1842 return base_partitions[split_idx];
1843}
1844
1845static inline void set_sb_size(SequenceHeader *const seq_params,
1846 BLOCK_SIZE sb_size) {
1847 seq_params->sb_size = sb_size;
1848 seq_params->mib_size = mi_size_wide[seq_params->sb_size];
1849 seq_params->mib_size_log2 = mi_size_wide_log2[seq_params->sb_size];
1850}
1851
1852// Returns true if the frame is fully lossless at the coded resolution.
1853// Note: If super-resolution is used, such a frame will still NOT be lossless at
1854// the upscaled resolution.
1855static inline int is_coded_lossless(const AV1_COMMON *cm,
1856 const MACROBLOCKD *xd) {
1857 int coded_lossless = 1;
1858 if (cm->seg.enabled) {
1859 for (int i = 0; i < MAX_SEGMENTS; ++i) {
1860 if (!xd->lossless[i]) {
1861 coded_lossless = 0;
1862 break;
1863 }
1864 }
1865 } else {
1866 coded_lossless = xd->lossless[0];
1867 }
1868 return coded_lossless;
1869}
1870
1871static inline int is_valid_seq_level_idx(AV1_LEVEL seq_level_idx) {
1872 return seq_level_idx == SEQ_LEVEL_MAX ||
1873 (seq_level_idx < SEQ_LEVELS &&
1874 // The following levels are currently undefined.
1875 seq_level_idx != SEQ_LEVEL_2_2 && seq_level_idx != SEQ_LEVEL_2_3 &&
1876 seq_level_idx != SEQ_LEVEL_3_2 && seq_level_idx != SEQ_LEVEL_3_3 &&
1877 seq_level_idx != SEQ_LEVEL_4_2 && seq_level_idx != SEQ_LEVEL_4_3
1878#if !CONFIG_CWG_C013
1879 && seq_level_idx != SEQ_LEVEL_7_0 && seq_level_idx != SEQ_LEVEL_7_1 &&
1880 seq_level_idx != SEQ_LEVEL_7_2 && seq_level_idx != SEQ_LEVEL_7_3 &&
1881 seq_level_idx != SEQ_LEVEL_8_0 && seq_level_idx != SEQ_LEVEL_8_1 &&
1882 seq_level_idx != SEQ_LEVEL_8_2 && seq_level_idx != SEQ_LEVEL_8_3
1883#endif
1884 );
1885}
1886
1889#ifdef __cplusplus
1890} // extern "C"
1891#endif
1892
1893#endif // AOM_AV1_COMMON_AV1_COMMON_INT_H_
int(* aom_get_frame_buffer_cb_fn_t)(void *priv, size_t min_size, aom_codec_frame_buffer_t *fb)
get frame buffer callback prototype
Definition aom_frame_buffer.h:64
int(* aom_release_frame_buffer_cb_fn_t)(void *priv, aom_codec_frame_buffer_t *fb)
release frame buffer callback prototype
Definition aom_frame_buffer.h:77
#define AOM_PLANE_U
Definition aom_image.h:211
enum aom_chroma_sample_position aom_chroma_sample_position_t
List of chroma sample positions.
enum aom_transfer_characteristics aom_transfer_characteristics_t
List of supported transfer functions.
enum aom_color_primaries aom_color_primaries_t
List of supported color primaries.
enum aom_matrix_coefficients aom_matrix_coefficients_t
List of supported matrix coefficients.
enum aom_bit_depth aom_bit_depth_t
Bit depth for codecThis enumeration determines the bit depth of the codec.
@ AOM_CODEC_CORRUPT_FRAME
The coded data for this stream is corrupt or incomplete.
Definition aom_codec.h:195
Top level common structure used by both encoder and decoder.
Definition av1_common_int.h:759
uint8_t * last_frame_seg_map
Definition av1_common_int.h:941
RestorationInfo rst_info[3]
Definition av1_common_int.h:955
WarpedMotionParams global_motion[REF_FRAMES]
Definition av1_common_int.h:979
int superres_upscaled_width
Definition av1_common_int.h:808
int8_t ref_frame_side[REF_FRAMES]
Definition av1_common_int.h:1046
struct scale_factors ref_scale_factors[REF_FRAMES]
Definition av1_common_int.h:878
RefCntBuffer * prev_frame
Definition av1_common_int.h:835
FRAME_CONTEXT * default_frame_context
Definition av1_common_int.h:996
int ref_frame_id[REF_FRAMES]
Definition av1_common_int.h:1021
int superres_upscaled_height
Definition av1_common_int.h:809
DeltaQInfo delta_q_info
Definition av1_common_int.h:974
SequenceHeader * seq_params
Definition av1_common_int.h:985
int width
Definition av1_common_int.h:784
RefCntBuffer * cur_frame
Definition av1_common_int.h:841
CdefInfo cdef_info
Definition av1_common_int.h:964
loop_filter_info_n lf_info
Definition av1_common_int.h:947
CurrentFrame current_frame
Definition av1_common_int.h:763
int remapped_ref_idx[REF_FRAMES]
Definition av1_common_int.h:863
RestorationLineBuffers * rlbs
Definition av1_common_int.h:957
aom_film_grain_t film_grain_params
Definition av1_common_int.h:969
int show_existing_frame
Definition av1_common_int.h:909
int temporal_layer_id
Definition av1_common_int.h:1052
struct aom_internal_error_info * error
Definition av1_common_int.h:767
int showable_frame
Definition av1_common_int.h:902
int tpl_mvs_mem_size
Definition av1_common_int.h:1035
uint32_t frame_presentation_time
Definition av1_common_int.h:830
uint32_t buffer_removal_times[(8 *4)+1]
Definition av1_common_int.h:824
struct loopfilter lf
Definition av1_common_int.h:948
int spatial_layer_id
Definition av1_common_int.h:1058
FeatureFlags features
Definition av1_common_int.h:914
struct scale_factors sf_identity
Definition av1_common_int.h:870
YV12_BUFFER_CONFIG rst_frame
Definition av1_common_int.h:958
CommonModeInfoParams mi_params
Definition av1_common_int.h:919
uint8_t superres_scale_denominator
Definition av1_common_int.h:816
int show_frame
Definition av1_common_int.h:894
struct segmentation seg
Definition av1_common_int.h:936
CommonQuantParams quant_params
Definition av1_common_int.h:931
TPL_MV_REF * tpl_mvs
Definition av1_common_int.h:1031
int current_frame_id
Definition av1_common_int.h:1020
int32_t * rst_tmpbuf
Definition av1_common_int.h:956
RefCntBuffer * ref_frame_map[REF_FRAMES]
Definition av1_common_int.h:887
CommonContexts above_contexts
Definition av1_common_int.h:1014
CommonTileParams tiles
Definition av1_common_int.h:1001
BufferPool * buffer_pool
Definition av1_common_int.h:1006
int ref_frame_sign_bias[REF_FRAMES]
Definition av1_common_int.h:1040
FRAME_CONTEXT * fc
Definition av1_common_int.h:990
int height
Definition av1_common_int.h:785
int render_width
Definition av1_common_int.h:795
int render_height
Definition av1_common_int.h:796
Parameters related to CDEF.
Definition av1_common_int.h:202
int cdef_bits
Number of CDEF strength values in bits.
Definition av1_common_int.h:224
int allocated_mi_rows
Number of rows in the frame in 4 pixel.
Definition av1_common_int.h:226
int allocated_num_workers
Number of CDEF workers.
Definition av1_common_int.h:228
size_t allocated_srcbuf_size
CDEF intermediate buffer size.
Definition av1_common_int.h:214
int nb_cdef_strengths
Number of CDEF strength values.
Definition av1_common_int.h:218
int cdef_damping
CDEF damping factor.
Definition av1_common_int.h:216
uint16_t * srcbuf
CDEF intermediate buffer.
Definition av1_common_int.h:208
Contexts used for transmitting various symbols in the bitstream.
Definition av1_common_int.h:722
PARTITION_CONTEXT ** partition
Definition av1_common_int.h:727
int num_planes
Definition av1_common_int.h:751
ENTROPY_CONTEXT ** entropy[3]
Definition av1_common_int.h:737
int num_tile_rows
Definition av1_common_int.h:752
int num_mi_cols
Definition av1_common_int.h:753
TXFM_CONTEXT ** txfm
Definition av1_common_int.h:745
Params related to MB_MODE_INFO arrays and related info.
Definition av1_common_int.h:510
int mb_cols
Definition av1_common_int.h:520
MB_MODE_INFO * mi_alloc
Definition av1_common_int.h:544
int mi_rows
Definition av1_common_int.h:531
void(* setup_mi)(struct CommonModeInfoParams *mi_params)
Definition av1_common_int.h:599
void(* free_mi)(struct CommonModeInfoParams *mi_params)
Definition av1_common_int.h:594
int mi_cols
Definition av1_common_int.h:536
int mi_alloc_size
Definition av1_common_int.h:548
void(* set_mb_mi)(struct CommonModeInfoParams *mi_params, int width, int height, BLOCK_SIZE min_partition_size)
Definition av1_common_int.h:609
int MBs
Definition av1_common_int.h:525
TX_TYPE * tx_type_map
Definition av1_common_int.h:584
int mi_alloc_stride
Definition av1_common_int.h:552
int mi_grid_size
Definition av1_common_int.h:572
int mi_stride
Definition av1_common_int.h:576
int mb_rows
Definition av1_common_int.h:515
MB_MODE_INFO ** mi_grid_base
Definition av1_common_int.h:568
BLOCK_SIZE mi_alloc_bsize
Definition av1_common_int.h:559
Parameters related to quantization at the frame level.
Definition av1_common_int.h:618
int u_ac_delta_q
Definition av1_common_int.h:643
const qm_val_t * u_iqmatrix[8][TX_SIZES_ALL]
Definition av1_common_int.h:692
int qmatrix_level_v
Definition av1_common_int.h:714
const qm_val_t * giqmatrix[(1<< 4)][3][TX_SIZES_ALL]
Definition av1_common_int.h:674
int16_t u_dequant_QTX[8][2]
Definition av1_common_int.h:663
const qm_val_t * y_iqmatrix[8][TX_SIZES_ALL]
Definition av1_common_int.h:688
int qmatrix_level_y
Definition av1_common_int.h:712
int v_ac_delta_q
Definition av1_common_int.h:648
bool using_qmatrix
Definition av1_common_int.h:705
int u_dc_delta_q
Definition av1_common_int.h:633
int qmatrix_level_u
Definition av1_common_int.h:713
int base_qindex
Definition av1_common_int.h:622
int16_t v_dequant_QTX[8][2]
Definition av1_common_int.h:664
const qm_val_t * v_iqmatrix[8][TX_SIZES_ALL]
Definition av1_common_int.h:696
int16_t y_dequant_QTX[8][2]
Definition av1_common_int.h:662
int v_dc_delta_q
Definition av1_common_int.h:637
int y_dc_delta_q
Definition av1_common_int.h:628
const qm_val_t * gqmatrix[(1<< 4)][3][TX_SIZES_ALL]
Definition av1_common_int.h:678
Params related to tiles.
Definition av1_common_int.h:436
int uniform_spacing
Definition av1_common_int.h:452
int max_width_sb
Definition av1_common_int.h:439
int log2_rows
Definition av1_common_int.h:459
int min_log2_rows
Definition av1_common_int.h:471
int width
Definition av1_common_int.h:460
int max_log2_rows
Definition av1_common_int.h:479
int row_start_sb[MAX_TILE_ROWS+1]
Definition av1_common_int.h:493
int cols
Definition av1_common_int.h:437
int max_height_sb
Definition av1_common_int.h:440
unsigned int large_scale
Definition av1_common_int.h:497
unsigned int single_tile_decoding
Definition av1_common_int.h:503
int max_log2_cols
Definition av1_common_int.h:475
int log2_cols
Definition av1_common_int.h:458
int min_log2
Definition av1_common_int.h:483
int rows
Definition av1_common_int.h:438
int min_inner_width
Definition av1_common_int.h:445
int min_log2_cols
Definition av1_common_int.h:467
int col_start_sb[MAX_TILE_COLS+1]
Definition av1_common_int.h:488
int height
Definition av1_common_int.h:461
Frame level features.
Definition av1_common_int.h:367
InterpFilter interp_filter
Definition av1_common_int.h:416
bool allow_ref_frame_mvs
Definition av1_common_int.h:390
bool allow_warped_motion
Definition av1_common_int.h:386
bool allow_screen_content_tools
Definition av1_common_int.h:384
bool switchable_motion_mode
Definition av1_common_int.h:414
TX_MODE tx_mode
Definition av1_common_int.h:415
bool reduced_tx_set_used
Definition av1_common_int.h:403
bool allow_intrabc
Definition av1_common_int.h:385
int byte_alignment
Definition av1_common_int.h:425
bool coded_lossless
Definition av1_common_int.h:394
REFRESH_FRAME_CONTEXT_MODE refresh_frame_context
Definition av1_common_int.h:430
bool error_resilient_mode
Definition av1_common_int.h:409
int primary_ref_frame
Definition av1_common_int.h:421
bool disable_cdf_update
Definition av1_common_int.h:371
bool allow_high_precision_mv
Definition av1_common_int.h:376
bool cur_frame_force_integer_mv
Definition av1_common_int.h:380
bool all_lossless
Definition av1_common_int.h:398
Stores the prediction/txfm mode of the current coding block.
Definition blockd.h:222
BLOCK_SIZE bsize
The block size of the current coding block.
Definition blockd.h:228
Parameters related to Restoration Info.
Definition restoration.h:246
External frame buffer.
Definition aom_frame_buffer.h:40
Variables related to current coding block.
Definition blockd.h:570
bool left_available
Definition blockd.h:626
uint8_t * tx_type_map
Definition blockd.h:666
int mb_to_bottom_edge
Definition blockd.h:680
TXFM_CONTEXT * left_txfm_context
Definition blockd.h:740
struct macroblockd_plane plane[3]
Definition blockd.h:606
int mb_to_top_edge
Definition blockd.h:679
int mb_to_right_edge
Definition blockd.h:678
bool up_available
Definition blockd.h:622
MB_MODE_INFO * above_mbmi
Definition blockd.h:645
bool chroma_up_available
Definition blockd.h:630
TXFM_CONTEXT * above_txfm_context
Definition blockd.h:733
bool chroma_left_available
Definition blockd.h:634
PARTITION_CONTEXT * above_partition_context
Definition blockd.h:718
MB_MODE_INFO * chroma_left_mbmi
Definition blockd.h:652
TXFM_CONTEXT left_txfm_context_buffer[MAX_MIB_SIZE]
Definition blockd.h:747
int tx_type_map_stride
Definition blockd.h:671
MB_MODE_INFO * chroma_above_mbmi
Definition blockd.h:659
int mi_row
Definition blockd.h:575
int mi_stride
Definition blockd.h:582
bool is_last_vertical_rect
Definition blockd.h:787
bool is_first_horizontal_rect
Definition blockd.h:792
uint8_t width
Definition blockd.h:765
struct aom_internal_error_info * error_info
Definition blockd.h:838
CFL_CTX cfl
Definition blockd.h:894
int lossless[8]
Definition blockd.h:817
ENTROPY_CONTEXT left_entropy_context[3][MAX_MIB_SIZE]
Definition blockd.h:710
ENTROPY_CONTEXT * above_entropy_context[3]
Definition blockd.h:703
MB_MODE_INFO ** mi
Definition blockd.h:617
uint8_t height
Definition blockd.h:766
MB_MODE_INFO * left_mbmi
Definition blockd.h:640
PARTITION_CONTEXT left_partition_context[MAX_MIB_SIZE]
Definition blockd.h:725
bool is_chroma_ref
Definition blockd.h:601
int mi_col
Definition blockd.h:576
int mb_to_left_edge
Definition blockd.h:677
YV12 frame buffer data structure.
Definition yv12config.h:46